package com.example.sherlockzhong.plush_v2.Utils;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.json.JSONTokener;

/**
 * Created by SherlockZhong on 9/9/14.
 */
public class NetworkUtils {

    private static HttpClient httpClient;
    private static int timeoutConnection = 10000;
    private static int timeoutSocket = 10000;

    public synchronized static String httpGet(String url) throws Exception {
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, timeoutConnection);
        HttpConnectionParams.setSoTimeout(params, timeoutSocket);
        httpClient = new DefaultHttpClient(params);
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                String responseString = EntityUtils.toString(httpResponse.getEntity());
                JSONTokener jsonTokener = new JSONTokener(responseString);
                JSONObject jsonObject = (JSONObject) jsonTokener.nextValue();
                if (jsonObject.get("status").toString().equalsIgnoreCase("200") && jsonObject.get("msg").toString().equalsIgnoreCase("ok")) {
                    return jsonObject.getString("data");
                } else {
                    return jsonObject.getString("msg");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public synchronized static JSONObject httpGetVerbose(String url) throws Exception {
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, timeoutConnection);
        HttpConnectionParams.setSoTimeout(params, timeoutSocket);
        httpClient = new DefaultHttpClient(params);
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                String responseString = EntityUtils.toString(httpResponse.getEntity());
                JSONTokener jsonTokener = new JSONTokener(responseString);
                JSONObject jsonObject = (JSONObject) jsonTokener.nextValue();
                return jsonObject;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static NetworkInfo getAvailableNetWorkInfo(Activity activity) {
        ConnectivityManager connectivityManager = (ConnectivityManager) activity
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        /* check network status */
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        /* check network status if it is available */
        if (activeNetInfo != null && activeNetInfo.isAvailable())
            return activeNetInfo;
        return null;
    }

}
